/*
 * @Author: 9ilk 3535851673@qq.com
 * @Date: 2024-11-17 10:13:03
 * @LastEditors: 9ilk 3535851673@qq.com
 * @LastEditTime: 2024-11-21 22:46:55
 * @FilePath: /zhuang/thread/mythread.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <thread>
#include <string>
#include <error.h>
#include <errno.h>
#include <string.h>
using namespace std;
string ToHex(pthread_t pd)
{
 char buffer[64];
 snprintf(buffer,sizeof(buffer),"0x%lx",pd);
 return buffer;
}

void *start1(void *args)
{
    string name = static_cast<const char *>(args);
    while (true)
    {
        cout << "I am a new thread : "<< name << ",thread is "<< ToHex(pthread_self())  << endl;
        sleep(1);
    }
    return nullptr;
}

int main()
{
    pthread_t td1;
    int* ret= nullptr;
    pthread_create(&td1, nullptr, start1, (void *)"thread-1");
    cout << "I am a main thread "<< ",thread is "<< ToHex(pthread_self()) 
    << " new Thread is "<< ToHex(td1) << endl;
    pthread_join(td1,nullptr);

}


// int *addr = nullptr;
// void *start1(void *args)
// {
//     string name = static_cast<const char *>(args);
//     int a = 100;
//     addr = &a;
//     while (true)
//     {
//         cout << name << " local val a: " << a << endl;
//         sleep(1);
//     }
// }

// void *start1(void *args)
// {
//     // pthread_detach(pthread_self()); //分离自己
//     string name = static_cast<const char *>(args);
//     while (true)
//     {
//         cout << "I am a new thread" << endl;
//         sleep(1);
//     }
//     pthread_exit((void*)10);
// }

// int main()
// {
//     pthread_t td1;
//     int* ret= nullptr;
//     pthread_create(&td1, nullptr, start1, (void *)"thread-1");
// //    pthread_detach(td1);
//     sleep(5);
//     // pthread_cancel(td1);
//     // cout << "取消线程 , tid:" << td1 << endl;
//     // sleep(5);

//     // pthread_create(&td2, nullptr, start2, (void *)"thread-2");
//     int n = pthread_join(td1,(void**)&ret);
//     cout << "thread exit code ret: "   << (long long)ret << ", n:" << n << endl;
//     // pthread_join(td2,nullptr);
//     return 0;
// }